home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13403 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: vixen.cso.uiuc.edu!e-torres
  2. From: e-torres@students.uiuc.edu (Edgar Luis Torres)
  3. Newsgroups: comp.lang.c++
  4. Subject: HELP: Overloading [] operator for multidimensional array of objects.
  5. Date: 25 Mar 1996 16:17:53 GMT
  6. Organization: University of Illinois at Urbana
  7. Message-ID: <4j6gvh$3n4@vixen.cso.uiuc.edu>
  8. NNTP-Posting-Host: ux5.cso.uiuc.edu
  9. Summary: How does one specify overloading the []operator for array of objects?
  10. Keywords: overload, subscript, multidimensional, arrays, operator
  11.  
  12. Hi!
  13.  
  14.     I would like to specify the overload of [], such that I may access
  15. a multidimensional array of objects. As an example, given I have a class
  16. EXAMCLASS and I wish to dynamically allocate a three-dimensional array of
  17. objects:
  18.     EXAMCLASS object1***;
  19.     ...
  20.     object1 = malloc( sizeof(object1), dim1sz * dim2sz * dim3sz );
  21. I know the above is sacrilege to C++ purists, but bare with me, since the malloc
  22. does work correctly in C++.  Now, what I want is to be able to:
  23.     object1[arg1][arg2][arg3].obj1Var1 = something;
  24. OR
  25.     something = object1[arg1][arg2][arg3].obj1Var1;
  26. In general, I know I have to include three friend operator in the definition of
  27. class EXAMCLASS, and then define them outside:
  28.     EXAMCLASS** operator[](EXAMCLASS*** C, int i){
  29.         return ((EXAMCLASS **)(C + (i * dim2sz * dim3sz))); }
  30.     EXAMCLASS* operator[](EXAMCLASS** C, int i){
  31.         return ((EXAMCLASS *)(C + (i * dim3sz))); }
  32.     EXAMCLASS operator[](EXAMCLASS* C, int i){
  33.         return *(C + i); }
  34. I know that the above does not work, but it does present the basic idea of what
  35. I am trying to do.  If anybody has the knowledge to help with the specifics of 
  36. syntax and argument rules, please HELP ME!  Somebody suggested replacing the *
  37. with &, but I am not sure what EXAMCLASS&& means??
  38.  
  39. Thanks in advance,
  40. Edgar
  41.  
  42.  
  43. --
  44. *()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*()*
  45. {   Edgar Luis Torres Silva    Electrical and Computer Engineering Dept    }
  46. {   1912A Orchard Street       Masters Graduate Student - Computer Eng     }
  47. {   Urbana, IL  61801          University of Illinois at Urbana-Champaign  }
  48.